package com.yuncheng.oaApi.controller;

import java.util.*;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaFlowNodeConstant;
import com.yuncheng.oaApi.entity.OaSysFlowNode;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.oaApi.service.IOaSpcySqsxJobService;
import com.yuncheng.oaApi.service.IOaSysFlowNodeService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaSysFlowTemple;
import com.yuncheng.oaApi.service.IOaSysFlowTempleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.system.api.SystemAPI;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 流程模板表
 * @Author: yuncheng
 * @Date: 2023-10-12
 * @Version: V1.0
 */
@Api(tags = "流程模板表接口")
@RestController
@RequestMapping("/api/spzx-oa/sysFlowTemple")
public class SysFlowTempleController extends BaseController<OaSysFlowTemple, IOaSysFlowTempleService> {

    private static final Logger log = LoggerFactory.getLogger(SysFlowTempleController.class);

    @Resource
    private IOaSysFlowTempleService sysFlowTempleService;

    @Resource
    @Lazy
    private SystemAPI systemAPI;

    @Resource
    @Lazy
    private IOaSysFlowNodeService sysFlowNodeService;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;


    @Resource
    @Lazy
    private IOaSpcySqsxJobService spcySqsxJobService;


    /**
     * 分页列表查询
     *
     * @param oaSysFlowTemple
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    @AutoLog(value = "流程模板表-分页列表查询")
    @ApiOperation(value = "流程模板表-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaSysFlowTemple oaSysFlowTemple, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<OaSysFlowTemple> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.like(StringUtils.isNotBlank(oaSysFlowTemple.getFywlx()), OaSysFlowTemple::getFywlx, oaSysFlowTemple.getFywlx()); //业务类型
        objectLambdaQueryWrapper.like(StringUtils.isNotBlank(oaSysFlowTemple.getFlcmc()), OaSysFlowTemple::getFlcmc, oaSysFlowTemple.getFlcmc()); //流程名称
        objectLambdaQueryWrapper.like(StringUtils.isNotBlank(oaSysFlowTemple.getFlcbm()), OaSysFlowTemple::getFlcbm, oaSysFlowTemple.getFlcbm()); //流程编码
        objectLambdaQueryWrapper.orderByAsc(OaSysFlowTemple::getFxh);
        Page<OaSysFlowTemple> page = new Page<>(pageNo, pageSize);
        IPage<OaSysFlowTemple> pageList = sysFlowTempleService.page(page, objectLambdaQueryWrapper);

        return HttpResult.ok(pageList);
    }



    /**
     * 添加
     *
     * @param oaSysFlowTemple
     * @return
     */
    @AutoLog(value = "流程模板表-添加")
    @ApiOperation(value = "流程模板表-添加")
    //@RequiresPermissions("FlowTemple:SysFlowTemple:add")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaSysFlowTemple oaSysFlowTemple) {
        HttpResult<String> result = new HttpResult(true);
        sysFlowTempleService.save(oaSysFlowTemple);
        result.setResult(oaSysFlowTemple.getId());
        String id = oaSysFlowTemple.getId();
        sysFlowNodeService.addFirstNode(id);
        //添加模板 添加日志
        spcyLogService.addLogs(oaSysFlowTemple.getFlcmc(), OaFlowNodeConstant.LOG_ADD_TEMPLE+":"+ oaSysFlowTemple.getFlcmc(),
                oaSysFlowTemple.getId(), OaFlowNodeConstant.LOG_ADD_TEMPLE, OaFlowNodeConstant.LOG_ADD_TEMPLE);

        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    /**
     * 编辑
     *
     * @param oaSysFlowTemple
     * @return
     */
    @AutoLog(value = "流程模板表-编辑")
    @ApiOperation(value = "流程模板表-编辑")
    //@RequiresPermissions("FlowTemple:SysFlowTemple:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaSysFlowTemple oaSysFlowTemple) {
        sysFlowTempleService.updateById(oaSysFlowTemple);

        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "流程模板表-通过id删除")
    @ApiOperation(value = "流程模板表-通过id删除")
    //@RequiresPermissions("FlowTemple:SysFlowTemple:delete")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        sysFlowTempleService.removeById(id);
        sysFlowNodeService.remove(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getFlcmbid, id));

        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "流程模板表-批量删除")
    @ApiOperation(value = "流程模板表-批量删除")
    //@RequiresPermissions("FlowTemple:SysFlowTemple:delete")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        sysFlowTempleService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "流程模板表-通过id查询")
    @ApiOperation(value = "流程模板表-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        OaSysFlowTemple oaSysFlowTemple = sysFlowTempleService.getById(id);
        return HttpResult.ok(oaSysFlowTemple);
    }






    /**
     * 数据转换
     * @param oaSysFlowTemple
     */
    private void formatValue(OaSysFlowTemple oaSysFlowTemple){
    }
}
